package com.igeek.compus.study.servlet;

import com.igeek.compus.study.entity.User;
import com.igeek.compus.study.service.UserService;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.net.URLEncoder;

@WebServlet(name = "userServlet", urlPatterns = {"/User"})
public class UserServlet extends HttpServlet {

    private UserService userService = new UserService();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String code = req.getParameter("code");
        if (code == null || (!"login".equals(code) && !"register".equals(code))) {
            //参数异常
            req.setAttribute("msg", "code参数异常");
            req.getRequestDispatcher("login.jsp").forward(req, resp);
        }

        try {
            if ("login".equals(code)) {
//              登录逻辑
                String username = req.getParameter("username");
                String password = req.getParameter("password");
//              执行登录业务
                User user = userService.login(username, password);
                if (user == null) {
                    req.setAttribute("msg", "用户不存在");
                    req.getRequestDispatcher("login.jsp").forward(req, resp);
                } else {
//                  设置免登陆
                    String loginTime_str = req.getParameter("loginTime");
                    if (loginTime_str == null) {
                        int loginTime = Integer.parseInt(loginTime_str);
//                      创建cookie
                        Cookie username_cookie=new Cookie("username", URLEncoder.encode(user.getUsername(),"utf8"));
                        Cookie password_cookie=new Cookie("password",user.getPassword());

                        username_cookie.setMaxAge(loginTime*24*3600);
                        password_cookie.setMaxAge(loginTime*24*3600);

                        resp.addCookie(username_cookie);
                        resp.addCookie(password_cookie);
                    }
                    // 登陆成功
                    // 在request中设置user对象，仅在同一个请求对象中有用
                    req.setAttribute("user", user.getUsername());

                    // 在session中设置user对象
                    HttpSession session = req.getSession();
                    session.setAttribute("user", user.getUsername());
                    req.getRequestDispatcher("home.jsp").forward(req, resp);

                    //获得应用上下文对象
                    ServletContext servletContext = this.getServletContext();
                    Object count = servletContext.getAttribute("count");
                    if (count == null){
                        servletContext.setAttribute("count",1);
                    }else {
                        servletContext.setAttribute("count",(int)count+1);
                    }

                    req.getRequestDispatcher("home.jsp").forward(req,resp);

                }


            } else {
                String username = req.getParameter("username");
                String password = req.getParameter("password");
                String sex = req.getParameter("sex");
                String phone = req.getParameter("phone");
                String email = req.getParameter("email");

                User user = new User();
                user.setUsername(username);
                user.setPassword(password);
                user.setSex(sex);
                user.setPhone(phone);
                user.setEmail(email);

                if (userService.register(user)) {
                    RequestDispatcher requestDispatcher = req.getRequestDispatcher("login.jsp");
                    req.setAttribute("username", user.getUsername());
                    requestDispatcher.forward(req, resp);
                } else {
                    RequestDispatcher requestDispatcher = req.getRequestDispatcher("register.jsp");
                    req.setAttribute("error", "系统错误");
                    requestDispatcher.forward(req, resp);
                }

            }


        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }
}
